package com.itrident.matcher;

import com.intellij.codeInsight.completion.PrefixMatcher;
import com.intellij.codeInsight.lookup.LookupElement;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cxx
 * @create 2020-12-29 9:27
 **/
public class MultipleMatcher extends PrefixMatcher {

    public static PrefixMatcher create(String prefix){
        return new MultipleMatcher (prefix);
    }

    private List<PrefixMatcher> matchers = new ArrayList<> ();

    protected MultipleMatcher(String prefix) {
        super (prefix);
//        matchers.add (UnderlinePrefixMatcher.create (prefix));
        matchers.add (new PrefixMatcher (prefix) {
            @Override
            public boolean prefixMatches(@NotNull String s) {
                return true;
            }

            @Override
            public @NotNull PrefixMatcher cloneWithPrefix(@NotNull String s) {
                return create(prefix);
            }
        });
    }

    @Override
    public boolean prefixMatches(@NotNull String name) {
        for (PrefixMatcher matcher : matchers) {
            if (matcher.prefixMatches (name)) {
                return true;
            }
        }
        return false;
    }

    @NotNull
    @Override
    public PrefixMatcher cloneWithPrefix(@NotNull String prefix) {
        return  create(prefix);
    }

    @Override
    public boolean isStartMatch(LookupElement element) {
        for (PrefixMatcher matcher : matchers) {
            boolean startMatch = matcher.isStartMatch (element);
            if(startMatch){
                return startMatch;
            }
        }
        return false;
    }

    @Override
    public boolean isStartMatch(String name) {
        for (PrefixMatcher matcher : matchers) {
            boolean startMatch = matcher.isStartMatch (name);
            if(startMatch){
                return startMatch;
            }
        }
        return false;
    }

    @Override
    public int matchingDegree(String string) {
        return super.matchingDegree (string);
    }
}
